import type { YAMLPlanetSchematics } from "../index";
import type { UniverseType } from "./UniverseType";

/**
 * 行星资源
 */
export class PlanetSchematics {
  /**
   * 物品ID
   */
  id!: number;
  /**
   * 名称
   */
  name!: string;
  /**
   * 分组ID
   */
  group_id!: number;
  /**
   * 输入
   */
  inputs!: { id: number; name: string; groupId: number; quantity: number }[];
  /**
   * 输出
   */
  output!: { id: number; name: string; groupId: number; quantity: number };
  /**
   * 生产周期
   */
  cycleTime!: number;
  /**
   * 可用于生产
   */
  pines!: UniverseType[];

  static fromYaml(yaml: YAMLPlanetSchematics, idMap: Map<number, UniverseType>, nameMap: Map<string, UniverseType>) {
    const planetSchematics = new PlanetSchematics();
    const type = nameMap.get(yaml.nameID.zh)!;
    planetSchematics.id = type.id;
    planetSchematics.name = type.name;
    planetSchematics.group_id = type.groupId;
    planetSchematics.inputs = Object.entries(yaml.types)
      .filter(([, type]) => type.isInput)
      .map(([id, type]) => ({
        quantity: type.quantity,
        name: idMap.get(Number(id))!.name,
        id: Number(id),
        groupId: idMap.get(Number(id))!.groupId,
      }));
    planetSchematics.output = Object.entries(yaml.types)
      .filter(([, type]) => !type.isInput)
      .map(([id, type]) => ({
        quantity: type.quantity,
        name: idMap.get(Number(id))!.name,
        id: Number(id),
        groupId: idMap.get(Number(id))!.groupId,
      }))[0];
    planetSchematics.cycleTime = yaml.cycleTime;
    planetSchematics.pines = yaml.pins.map((id) => idMap.get(id)!);
    return planetSchematics;
  }
}
